local xiemu = fk.CreateSkill{
    name = "wk_heg__xiemu",
}

Fk:loadTranslationTable{
  ["wk_heg__xiemu"] = "协穆",
  [":wk_heg__xiemu"] = "出牌阶段限一次，你可以将一张<font color='red'>♥</font>牌当【五谷丰登】对本回合所有成为过你使用牌的目标使用，然后这些角色可以依次使用一张牌。",

  ["#wk_heg__xiemu_prompt"] = "协穆：你可以将一张 <font color='red'>♥</font> 牌当【五谷丰登】对本回合成为过你是使用牌的目标使用",
  ["wk_heg__xiemu_use"] = "协穆：你可以使用一张牌",

  ["$wk_heg__xiemu1"] = "休要再起战事。",
  ["$wk_heg__xiemu2"] = "暴戾之气，伤人害己。",
}


xiemu:addEffect("active",{
  anim_type = "support",
  prompt = "#wk_heg__xiemu_prompt",
  can_use = function(self, player)
    return player:usedSkillTimes(xiemu.name, Player.HistoryPhase) == 0 and #table.filter(Fk:currentRoom().alive_players, function (p) return p:getMark("_wk_heg__xiemu-turn") > 0 end) > 0
     and player:canUse(Fk:cloneCard("amazing_grace"))
  end,
  card_num = 1,
  card_filter = function(self, player, to_select, selected)
    return #selected < 1 and Fk:getCardById(to_select).suit == Card.Heart
  end,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = effect.from
    local card = effect.cards
    local tos = table.filter(room:getAlivePlayers(false), function (p) return p:getMark("_wk_heg__xiemu-turn") > 0 end)
    room:useVirtualCard("amazing_grace", card, player, tos, xiemu.name)
    room:sortByAction(tos)
    for _, p in ipairs(tos) do
      if not p.dead then
        room:askToPlayCard(p, {skill_name = xiemu.name, prompt = "wk_heg__xiemu_use", extra_data = {bypass_times = true}})
      end
    end
  end,
})

xiemu:addEffect(fk.TargetConfirmed,{
  can_refresh = function(self, event, target, player, data)
    if not (player:hasSkill(xiemu.name) or player:isFakeSkill(xiemu.name)) then return false end
    return data.from == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(target, "_wk_heg__xiemu-turn", 1)
  end,
})

xiemu:addEffect(fk.EventAcquireSkill,{
  can_refresh = function(self, event, target, player, data)
    if not (player:hasSkill(xiemu.name) or player:isFakeSkill(xiemu.name)) then return false end
    return target == player and data == xiemu
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if room:getTag("RoundCount") then
      room.logic:getEventsOfScope(GameEvent.UseCard, 99, function (e)
        local use = e.data
        if use.from == player then
          for _, p in ipairs(use.tos) do
            if p:getMark("_wk_heg__xiemu-turn") == 0 then
              room:setPlayerMark(p, "_wk_heg__xiemu-turn", 1)
            end
          end
        end
        end, Player.HistoryTurn)
      end
  end,
})

return xiemu